package com.demo.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.demo.constant.CacheConstants;
import com.demo.domain.SysUser;
import com.demo.service.SysDictService;
import com.demo.service.SysUserService;
import com.demo.utils.AjaxResult;
import com.demo.utils.BaseController;
import com.demo.utils.PageUtils;
import com.demo.utils.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author gxd
 * @since 2025/5/27
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户信息")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysDictService dictService;

    @ApiOperation("分页列表-用户列表")
    @PostMapping("/pageList")
    public TableDataInfo pageList(@ApiParam(value = "用户查询条件", required = false) SysUser user) {
        PageUtils.startPage(user.getPageNum(), user.getPageSize());
        List<SysUser> userList = userService.pageList(user);
        return getDataTable(userList);
    }

    @ApiOperation("不分页列表-用户列表")
    @PostMapping("/list")
    public AjaxResult list(@ApiParam(value = "用户查询条件", required = false) @RequestBody SysUser user) {
        return success(userService.selectUserList(user));
    }

    @ApiOperation("详情信息-根据Id获取用户详情信息")
    @ApiImplicitParam(name = "id", value = "用户Id", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getById/{id}")
    public AjaxResult getById(@PathVariable(value = "id") String id) {
        SysUser user = userService.getById(id);
        if (ObjUtil.isNotNull(user)) {
            Map<String, String> sexDictMap = dictService.getDictMapByCode(CacheConstants.USER_SEX_CODE);
            Map<String, String> delFlagDictMap = dictService.getDictMapByCode(CacheConstants.DELETE_FLAG_CODE);
            user.setSexMc(sexDictMap.get(user.getSex()));
            user.setDelFlagMc(delFlagDictMap.get(user.getDelFlag()));
        }
        return success(user);
    }

    @ApiOperation("添加或修改-用户信息")
    @PostMapping("/addOrEdit")
    public AjaxResult addOrEdit(@ApiParam(value = "用户实体", required = true) @Valid @RequestBody SysUser user) {
        return success(userService.addOrEdit(user));
    }

    @ApiOperation("删除-用户信息（支持批量）")
    @ApiImplicitParam(name = "ids", value = "用户ID（多个需逗号分隔）", required = true, allowMultiple = true, dataType = "String", paramType = "path")
    @DeleteMapping("/removeById")
    public AjaxResult removeById(@RequestParam("ids") String ids) {
        Assert.notEmpty(ids, "缺少必要参数：请提供要删除的记录 ID");
        return toAjax(userService.update(Wrappers.lambdaUpdate(SysUser.class)
                .set(SysUser::getDelFlag, "1").in(SysUser::getUserId, ids.split(StrPool.COMMA))));
    }

    @ApiOperation("关联查询-用户列表")
    @GetMapping("/selectUserJoin")
    public AjaxResult selectUserJoin(@ApiParam(value = "用户查询条件", required = false) SysUser sysUser) {
        PageUtils.startPage(sysUser.getPageNum(), sysUser.getPageSize());
        return success(userService.selectUserJoin());
    }

    @ApiOperation("分批查询-分批查询-当天每半小时一分组，求每个半小时的总数")
    @GetMapping("/selectUserBatch")
    public AjaxResult selectUserBatch() {
        return success(userService.selectUserBatch());
    }

    @ApiOperation("批量更新-拼音字段")
    @PostMapping("/batchUpdatePinyin")
    public AjaxResult batchUpdatePinyin() {
        return toAjax(userService.batchUpdatePinyin());
    }


    @ApiOperation("拼音查询-用户名列表")
    @PostMapping("/selectUserNameByPinyin")
    public AjaxResult selectUserNameByPinyin(@ApiParam(value = "用户查询条件", required = false) SysUser user) {
        return success(userService.selectUserNameByPinyin(user));
    }
}
